package com.mxfcode.front.bbs_answer_questions.controller;

import com.mxfcode.bbs_common.util.AOSUtils;
import com.mxfcode.bbs_common.util.community.CommunityConstant;
import com.mxfcode.bbs_common.util.page.Page;
import com.mxfcode.entity.BBSAnswerQuestions;
import com.mxfcode.entity.BBSComment;
import com.mxfcode.entity.TSUser;
import com.mxfcode.front.bbs_answer_questions.service.AnswerQuestionsService;
import com.mxfcode.front.bbs_comment.service.CommentService;
import com.mxfcode.front.bbs_login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author moxiufeng
 * @date 2021/2/27 23:03
 */
@Controller
@RequestMapping("/answerQuestions")
public class AnswerQuestionsController implements CommunityConstant {
    @Autowired
    private AnswerQuestionsService answerQuestionsService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private CommentService commentService;

    @RequestMapping(path = "/list",method = RequestMethod.GET)
    public String goArticleListPage(Model model, Page page, HttpServletRequest request){
        //type == 0 搜索全部，type == 1，按点赞排序，type == 2,按回答人数排序，type == 3 按创建时间排序
        String type = request.getParameter("type");
        page.setRows(answerQuestionsService.selectAnswerQuestionsRows(type));
        page.setPath("/answerQuestions/list");
        List<BBSAnswerQuestions> list = answerQuestionsService.selectAnswerQuestionsList(type, page.getOffset(), page.getLimit(), 0);
        List<Map<String,Object>> dataList = new ArrayList<>();
        if(AOSUtils.isNotEmpty(list)){
            int i = 1;
            for (BBSAnswerQuestions answer : list ){
                Map<String,Object> map = new HashMap<>();
                map.put("answerQuestions",answer);
                String userId = answer.getUserId();
                TSUser user = loginService.findUserById(userId);
                map.put("user",user);
                map.put("xh",i);
                i++;
                dataList.add(map);
            }
        }
        model.addAttribute("type",type);
        model.addAttribute("dataList",dataList);
        return "/answer_questions";
    }

    @GetMapping(path = "/detail/{messageId}")
    public String toArticleDetail(@PathVariable("messageId")String messageId, Model model, Page page){
        BBSAnswerQuestions answerQuestions = answerQuestionsService.selectMessageById(messageId);
        String userId = answerQuestions.getUserId();
        TSUser user = loginService.findUserById(userId);
        model.addAttribute("user",user);
        model.addAttribute("answerQuestions",answerQuestions);
        //评论分页信息
        page.setLimit(5);
        page.setPath("/answerQuestions/detail/" + messageId);
        page.setRows(Integer.valueOf(answerQuestions.getCommentCount()+""));

        // 评论: 给帖子的评论
        // 回复: 给评论的评论
        // 评论列表
        List<BBSComment> commentList = commentService.findCommentsByEntity(
                ENTITY_TYPE_POST_AQ, messageId, page.getOffset(), page.getLimit());
        // 评论VO列表
        List<Map<String, Object>> commentVoList = new ArrayList<>();
        if (commentList != null) {
            for (BBSComment comment : commentList) {
                // 评论VO
                Map<String, Object> commentVo = new HashMap<>();
                // 评论
                commentVo.put("comment", comment);
                // 作者
                commentVo.put("user", loginService.findUserById(comment.getUserId()));
                // 回复列表
                List<BBSComment> replyList = commentService.findCommentsByEntity(
                        ENTITY_TYPE_COMMENT, comment.getId().toString(), 0, Integer.MAX_VALUE);
                // 回复VO列表
                List<Map<String, Object>> replyVoList = new ArrayList<>();
                if (replyList != null) {
                    for (BBSComment reply : replyList) {
                        Map<String, Object> replyVo = new HashMap<>();
                        // 回复
                        replyVo.put("reply", reply);
                        // 作者
                        replyVo.put("user", loginService.findUserById(reply.getUserId()));
                        // 回复目标
                        TSUser target = reply.getTargetId() == null ? null : loginService.findUserById(reply.getTargetId());
                        replyVo.put("target", target);

                        replyVoList.add(replyVo);
                    }
                }
                commentVo.put("replys", replyVoList);

                // 回复数量
                int replyCount = commentService.findCommentCount(ENTITY_TYPE_COMMENT, comment.getId().toString());
                commentVo.put("replyCount", replyCount);

                commentVoList.add(commentVo);
            }
        }

        model.addAttribute("comments", commentVoList);

        return "/answer_questions_read";
    }
}
